<?php
header("Content-Type: text/html; charset=iso-8859-1");
require('include.php');
$_SESSION['navigation'] = 'membres';
$user_id = check_cookie();
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : "nom";
if(preg_match("/(.*)\ (.*)/",$order,$regs)){
	$order = ($regs[2] == 'asc') ? $regs[1]." desc" : $regs[1]." asc";
}

$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
$start = (!empty($_REQUEST['start'])) ? $_REQUEST['start'] : 1;
$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
$box = (!empty($_REQUEST['box'])) ? $_REQUEST['box'] : 0;
$marquage = (!empty($_REQUEST['marquage'])) ? $_REQUEST['marquage'] : 0;
$arbitrage = (!empty($_REQUEST['arbitrage'])) ? $_REQUEST['arbitrage'] : 0;
if(isset($_REQUEST['filtre_prenom']) && isset($_SESSION['filtre_prenom'])) unset($_SESSION['filtre_prenom']);
if(isset($_REQUEST['filtre_nom']) && isset($_SESSION['filtre_nom'])) unset($_SESSION['filtre_nom']);
$filtre_prenom = (!empty($_REQUEST['filtre_prenom'])) ? $_REQUEST['filtre_prenom'] : "";
$filtre_titre = (!empty($_REQUEST['filtre_titre'])) ? $_REQUEST['filtre_titre'] : "";
#$_SESSION['filtre_prenom'] = ($filtre_prenom) ? $filtre_prenom : $_SESSION['filtre_prenom'];
#$_SESSION['filtre_nom'] = ($filtre_nom) ? $filtre_nom : $_SESSION['filtre_nom'];

if($action == 'detail' && $id){
	$membre = new membre($id);
	$membre->print_resume($box,$marquage,$arbitrage);
}
elseif($action == 'list'){
	$table = new table();
	$fields = array("prenom" => "pr&eacute;nom",
					"nom" => "nom",
					"equipes" => "&eacute;quipe(s)");
	foreach($fields as $field => $print){
		$$field = new table_field($field);
		$$field->print = $print;
		$table->fields[] = $$field;
	}
	$equipes->filter = 0;
	$table->styleclass = 'hover';
	$table->filter = 1;
	$q = "select membres.id as membre, equipes.nom as equipe from membres left join membre_equipes on membres.id = membre_id inner join equipes on equipes.id = equipe_id order by membre_id";
	$r = ExecRequete($q);
	while($s = LigneSuivante($r)){
		${$s->membre}[] = $s->equipe;
	}
	$q = "select membres.id as id, prenom,membres.nom as nom, membres.note as note from membres where valide = 1 and fictif = 0 order by membres.nom,membres.prenom ";
	$r = ExecRequete($q);
	while($s = LigneSuivante($r)){
		$row = new table_row();
		$row->styleclass = 'hover';
		$row->onclick = "onclick = \"membre_details($s->id);self.location.href='#detail';\"";
		foreach($fields as $field => $print){
			if($field == 'equipes'){
				$echo = (count(${$s->id})) ? implode(", ",${$s->id}) : "";
				if(!$echo && $s->note) $echo = $s->note;
			}
			else{
				$echo = CapitalizeLastName(strtolower($s->$field));
			}
			$row->cols[$field] = $echo;
		}
		$table->rows[] = $row;
	}
	echo $table->print_header();
	echo $table->print_body();
	echo $table->print_footer();
}
else{
	entete("VBCLL membres");
	$_SESSION['navigation'] = 'membres';
	if(!$id) $id = $user_id;
	if($action == 'complet'){
		$membre = new membre($id);
		if($membre->valide || check_admin()){
			$edit = ($id == $user_id || check_admin()) ? "<span class = 'float_right'><a href = 'membre.php?id=$id&amp;action=edit'><img src = 'images/icon_edit.png' alt = 'edit' width = '16' height = '16' /></a></span>" : "";
			echo "<h1>".(CapitalizeLastName(strtolower($membre->prenom." ".$membre->nom)))."$edit</h1>\n";
			if(check_cookie()){
				$membre->print_detail();
				if($id == $user_id){
					echo "<p class = 'center'><button type = 'button' onclick = 'self.location.href=\"membre.php?id=$id&amp;action=edit\"'><img src = 'images/icon_edit.png' alt = '' width = '14' height = '14' />modifier mes coordonn&eacute;es</button></p>\n";
				}
				echo "<h2>Calendrier<span class = 'float_right'><pre id = 'nom_cal' style = \"visibility:hidden; display: none;padding-right: 1em;vertical-align: top;font-size: 9pt;\">fichier iCal</pre><a href = 'ical.php?id=$membre->nom'><img src = 'images/icon_ical.png' alt = 'ical' title = 'ical' height = '23' width = '23' onmouseover = \"show_el('nom_cal')\" onmouseout = \"hide_el('nom_cal')\"/></a></span></h2>\n";
				$membre->calendrier();
			}
			else{
				$membre->print_resume(1);
			}
		}
	}
	elseif($action == 'print_detail' && $id){
		$membre = new membre($id);
		if($membre->valide || check_admin()){
			$edit = ($id == $user_id || check_admin()) ? "<span class = 'float_right'><a href = 'membre.php?id=$id&amp;action=edit'><img src = 'images/icon_edit.png' alt = 'edit' width = '16' height = '16' /></a></span>" : "";
			echo "<h1>".(CapitalizeLastName(strtolower($membre->prenom." ".$membre->nom)))."$edit</h1>\n";
			$membre->print_resume(1);
		}
	}
	elseif($action == 'edit'){
		$membre = new membre($id);
		$edit = ($id == $user_id || check_admin()) ? "<span class = 'float_right'><a href = 'membre.php?id=$id&amp;action=complet'><img src = 'images/icon_ok.png' alt = 'edit' width = '16' height = '16' /></a></span>" : "";
		echo "<h1>".(CapitalizeLastName(strtolower($membre->prenom." ".$membre->nom)))."$edit</h1>\n";
		$value = ($id == $user_id || check_admin()) ? 1 : 0;
		$membre->print_detail($value);
		echo "<p class = 'center'><button type = 'button' onclick = 'self.location.href=\"membre.php?id=$id&action=complet\"'><img src = 'images/icon_ok.png' alt = 'edit' width = '16' height = '16' />retour &agrave; la page du membre</button></p>\n";
	}
	else{
		echo "<h1>Les membres</h1>\n";
		echo "<div class = 'hidden' style = 'margin-left: 60px; height: 150px; display: none;' id = 'membre_details'><a name = 'detail'></a>\n";
		box("<div id = 'nom'></div>","<div id = 'details'></div>",500,1);
		echo "</div>\n";
		echo "<div id = 'action'></div>\n";
		echo "<script type = 'text/javascript'>list_membres(1);</script>\n";
	}
	pied();
}
?>